//
//  ITAgreementView.m
//  InVid
//
//  Created by seven on 2020/10/13.
//  Copyright © 2020 InVid. All rights reserved.
//

#import "ITAgreementView.h"
#define AgreementKey @"AgreementKey"
@interface ITAgreementView(){
    UIButton *_agreeBtn;
}
@end

@implementation ITAgreementView

+ (instancetype)showWithAction:(CustomAction)action{
    if ([NSUserDefaults getValueForKey:AgreementKey]) {
        return nil;
    }
    return [super showWithAction:action];
}

- (void)initViews{
    [super initViews];
    
    [self.contentView setCornerRadius:18];
    self.contentView.backgroundColor = COLOR_WHITE;
    self.contentView.frame = CGRectMake(55, 0, self.width-110, 10);
    self.closeBtn.hidden = YES;
    
    CALayer *icon = [CALayer layerWithImage:@"agreement" frame:CGRectMake((self.contentView.width-109)/2, 34, 109, 82)];
    [self.contentView.layer addSublayer:icon];
    
    
    BaseLabel *lbl = self.titleLbl;
    lbl.frame = CGRectMake(18, icon.bottom+22, self.contentView.width-36, 82);
    lbl.numberOfLines = 0;
    NSString *tmp = [AppUtils titleForKey:@"agreement"];
    NSString *str = [AppUtils titleForKey:@"In order to use the APP normally, please agree to the following user %@",tmp];
    NSMutableAttributedString *string = [NSAttributedString generateAttriuteStringWithStr:str WithColor:COLOR_333333 WithFont:MediumFontWithSize(18)];
    NSRange range = [str rangeOfString:tmp];
    NSDictionary *a = @{
        NSForegroundColorAttributeName: ColorWithHex(0x008EFF),
        NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
    };
    [string setAttributes:a range:range];
    string.alignment = NSTextAlignmentCenter;
    lbl.attributedText = string;
    [lbl fitWidth:lbl.width];
    
    WEAKSELF
    lbl.customAction = ^(id  _Nullable obj, CustomActionType idx) {
        if (idx == cat_tap) {
            NSRange r = [obj[@"range"] rangeValue];
            if (r.length == range.length && r.location == range.location) {
                [weak_self goAgreement];
            }
        }
    };
    
    UIButton *btn = [UIButton initWithFrame:CGRectMake(0, lbl.bottom, self.contentView.width, 48)];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 32, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    btn.titleLabel.font = MediumFontWithSize(15);
    [btn setTitleColor:COLOR_333333];
    [btn setImageWithName:@"agreement_ds"];
    [btn setSelectedImageWithName:@"agreement_s"];
    [btn addTarget:self action:@selector(onAgreement)];
    [btn setTitleNormal:LocalTitle(@"You agree to all above content")];
    [self.contentView addSubview:btn];
    _agreeBtn = btn;
    
    btn = self.okBtn;
    btn.frame = CGRectMake(61, _agreeBtn.bottom+10, self.contentView.width-122, 44);
    [btn setCornerRadius:btn.height/2];
    btn.backgroundColor = COLOR_BLUE;
    btn.titleLabel.font = MediumFontWithSize(18);
    [btn setTitleColor:COLOR_WHITE];
    [btn setTitleNormal:LocalTitle(@"Confirm")];
    self.contentHeight = btn.bottom+32;
}

- (void)onDoneAction{
    if (!_agreeBtn.selected) {
        [AppUtils showInfo:LocalTitle(@"Please agree above content")];
        [_agreeBtn.imageView shakeAnimation2];
        return;
    }
    [NSUserDefaults saveValue:@"1" forKey:AgreementKey];
    [self _onDoneAction];
}

- (void)onAgreement{
    _agreeBtn.selected = !_agreeBtn.isSelected;
    if (_agreeBtn.selected) {
        [_agreeBtn.imageView.layer removeAllAnimations];
    }
}

- (void)goAgreement{
    [AppUtils pushWebView:[AppUtils configDicWithKey:DICT_WEB_PRIVACY] title:@"Terms of Service"];
}

@end
